package net.pierrerenaudin.blobbyvolley.client;

import com.google.gwt.dom.client.Style.Unit;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.ui.AbsolutePanel;
import com.google.gwt.user.client.ui.Anchor;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.VerticalPanel;

public class UIManager {

	private GameController gameController;

	private AbsolutePanel mainPanel;

	public UIManager(){
		gameController = new GameController(this);

		// Page Display
		RootPanel.getBodyElement().getStyle().setBackgroundColor("#eeeeee");
		mainPanel = new AbsolutePanel();
		mainPanel.setHeight("600px");
		mainPanel.setWidth("800px");
		mainPanel.getElement().getStyle().setProperty("margin", "0 auto");
		mainPanel.getElement().getStyle().setMarginTop(30,Unit.PX);
		mainPanel.getElement().getStyle().setBackgroundImage("data/img/strand1.png");
		RootPanel.get().add(mainPanel);
	}

	public void startMenu(){
		mainPanel.clear();

		Image backgroundMenuImage = new Image("data/img/menu-background.jpg");
		mainPanel.add(backgroundMenuImage,0,0);

		// Display Menu
		Anchor startAnchor = new Anchor();
		startAnchor.setText("Start Game !");
		startAnchor.addStyleName("anchor-link-style");
		startAnchor.addClickHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				startGame();
			}
		});
		mainPanel.add(startAnchor,40,450);

		// Pref Panel
		VerticalPanel prefPanel = new VerticalPanel();
		// Sound mute
		HorizontalPanel mutePanel = new HorizontalPanel();
		Label soundLabel = new Label("Sound :    ");
		soundLabel.getElement().getStyle().setMarginRight(20, Unit.PX);
		soundLabel.addStyleName("blobby-style");
		mutePanel.add(soundLabel);
		final Anchor muteAnchor = new Anchor();
		muteAnchor.setText("On");
		muteAnchor.addStyleName("anchor-link-style");
		muteAnchor.addClickHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				if(muteAnchor.getText().equals("On")){
					muteAnchor.setText("Mute");
					gameController.getSoundManager().setMute(true);
				}
				else{
					muteAnchor.setText("On");
					gameController.getSoundManager().setMute(false);
				}
			}
		});
		mutePanel.add(muteAnchor);
		prefPanel.add(mutePanel);

		// Keyboard
		HorizontalPanel keyboardPanel = new HorizontalPanel();
		Label keyboardLabel = new Label("Keyboard :");
		keyboardLabel.getElement().getStyle().setMarginRight(8, Unit.PX);
		keyboardLabel.addStyleName("blobby-style");
		keyboardPanel.add(keyboardLabel);
		final Anchor keyboardAnchor = new Anchor();
		keyboardAnchor.setText("AZERTY");
		keyboardAnchor.addStyleName("anchor-link-style");
		
		keyboardPanel.add(keyboardAnchor);
		prefPanel.add(keyboardPanel);
		
		//LeftPlayer
		final Label leftPlayerLabel = new Label("Left Player : Z - Q - D");
		leftPlayerLabel.addStyleName("blobby-style");
		prefPanel.add(leftPlayerLabel);
		Label rightPlayerLabel = new Label("Right Player : UP - LEFT - RIGHT");
		rightPlayerLabel.addStyleName("blobby-style");
		prefPanel.add(rightPlayerLabel);
		
		keyboardAnchor.addClickHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				if(keyboardAnchor.getText().equals("AZERTY")){
					keyboardAnchor.setText("QWERTY");
					gameController.getKeyboardManager().setQwertyKeyboard(true);
					leftPlayerLabel.setText("Left Player : A - W - D");
				}
				else{
					keyboardAnchor.setText("AZERTY");
					gameController.getKeyboardManager().setQwertyKeyboard(false);
					leftPlayerLabel.setText("Left Player : Z - Q - D");
				}
			}
		});
		
		mainPanel.add(prefPanel,430,420);
		
		// Copyright
		Label copyrightLabel = new Label("Pierre Renaudin - March 2011");
		copyrightLabel.addStyleName("blobby-style");
		copyrightLabel.getElement().getStyle().setFontSize(11, Unit.PT);
		mainPanel.add(copyrightLabel,10,580);
	}

	public void startGame(){
		mainPanel.clear();

		// Add Game Canvas
		mainPanel.add(gameController.getGameCanvas());
		gameController.resetRuleVars();
		gameController.start();
	
		// Back to main menu
		Anchor backToMainMenuAnchor = new Anchor();
		backToMainMenuAnchor.setText("Close");
		backToMainMenuAnchor.addStyleName("anchor-link-small");
		backToMainMenuAnchor.addClickHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				startMenu();
			}
		});
		mainPanel.add(backToMainMenuAnchor, 680, 550);
	}
	
	public void startWin(Player winPlayer){
		gameController.pause();
		mainPanel.clear();

		// Add Game Canvas
		mainPanel.add(gameController.getGameCanvas());
		gameController.getRenderManager().drawBackgroundWin(winPlayer);
		Label winPlayerLabel;
		if(winPlayer.side == Player.LEFT_PLAYER){
			winPlayerLabel = new Label("Left Player has won the Game !!!");
		}
		else{
			winPlayerLabel = new Label("Right Player has won the Game !!!");
		}
		winPlayerLabel.addStyleName("blobby-style");
		mainPanel.add(winPlayerLabel,380,160);
		
		// Back to main menu
		Anchor backToMainMenuAnchor = new Anchor();
		backToMainMenuAnchor.setText("Back to main menu");
		backToMainMenuAnchor.addStyleName("anchor-link-small");
		backToMainMenuAnchor.addClickHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				startMenu();
			}
		});
		mainPanel.add(backToMainMenuAnchor, 630, 500);
	}


}
